
package com.google.code.vh;

import com.vaadin.Application;
import com.vaadin.service.ApplicationContext;

public class App extends Application implements ApplicationContext.TransactionListener
{
  private static ThreadLocal<App> d_currentApplication = new ThreadLocal<App>();

  @Override
  public void transactionStart( Application application, Object o )
  {
    if ( application == App.this )
    {
      d_currentApplication.set( this );
    }
  }

  @Override
  public void transactionEnd( Application application, Object o )
  {
    if ( application == App.this )
    {
      d_currentApplication.set( null );
      d_currentApplication.remove();
    }
  }

  public static App getInstance()
  {
    return d_currentApplication.get();
  }

  @Override
  public void init()
  {
    getContext().addTransactionListener( this );
    setMainWindow( new LoginWindow() );
  }

  public void authenticate( String login, String password ) throws Exception
  {
    if ( !login.equals( "user" ) && !password.equals( "qwerty" ) )
    {
      throw new Exception( "Login failed !" );
    }

    loadProtectedResources();
  }

  private void loadProtectedResources()
  {
    setMainWindow( new MainWindow() );
  }
}
